/**
 * Created by Scor on 2015/9/1.
 */
//AngularJS 控制器
var grApp = angular.module('gr-app', []);
grApp.controller('CouponCtrl',['$scope',
    '$http',
    '$location',
    'CouponFactory',
    function($scope,$http,$location,CouponFactory){
    //页面初始化载入顺序事件
        var timestamp = Date.parse(new Date());
        var loadingModal=angular.element("#icon-alert");
        var getCouponErr=angular.element("#getCouponErr");
        var getCouponAlert=angular.element("#getCouponAlert");
        $scope.couponCode="";
        $scope.placeholder="请输入6位兑换码";
        //加载按钮开始
        loadingModal.modal();
        //获取优惠券列表
        CouponFactory.getUserCouponsList().then(function(data) {  // 调用承诺API获取数据 .resolve
            $scope.xjCoupon= new Array();
            $scope.mjCoupon= new Array();
            $scope.allCoupon=data;
            $scope.couponNum=$scope.allCoupon.length;
            $scope.couponCode="";
            if(data.length>0){
                //2满减，1现金区分
                angular.forEach($scope.allCoupon,function(data,index,array){
                    if((parseInt(data.validityEndPeriod))==0||(parseInt(data.validityEndPeriod))==undefined){
                        //如果个人优惠券没有时间限制则限制时间为优惠券失效时间
                        data.coupon.vaildDays=parseInt((parseInt(data.coupon.validityEndPeriod)-timestamp)/86400000);
                    }else{
                        //否则使用个人优惠券失效时间
                        data.coupon.vaildDays=parseInt((parseInt(data.validityEndPeriod)-timestamp)/86400000);
                    }
                    if(data.coupon.type=="1"){
                        $scope.xjCoupon.push(data.coupon);
                    }else if(data.coupon.type=="2"){
                        $scope.mjCoupon.push(data.coupon);
                    }
                });
            }
            //加载按钮结束
            loadingModal.modal('close');
        });

        $scope.getCouponAlert=function(){
            getCouponAlert.modal();
        };
        //判断优惠券格式
        $scope.checkCouponForm=function(){
            getCouponAlert.modal("close");
            var code=$scope.couponCode;
            if(code==""||code.length!=6){
                $scope.couponCode="";
                $scope.errContent="兑换码格式不正确"
                getCouponErr.modal();
                return false;
            }
            loadingModal.modal();
            //领取优惠券
            CouponFactory.getCouponCode(code).then(function(data){
                console.log(data);
                loadingModal.modal('close');
                switch (data.code){
                    case 0:
                        $scope.errContent="恭喜你，获得一张优惠券";
                        //刷新优惠券列表
                        if((parseInt(data.res.validityEndPeriod))==0||(parseInt(data.res.validityEndPeriod))==undefined){
                            //如果个人优惠券没有时间限制则限制时间为优惠券失效时间
                            data.res.coupon.vaildDays=parseInt((parseInt(data.res.coupon.validityEndPeriod)-timestamp)/86400000);
                        }else{
                            //否则使用个人优惠券失效时间
                            data.res.coupon.vaildDays=parseInt((parseInt(data.res.validityEndPeriod)-timestamp)/86400000);
                        }
                        if(data.res.coupon.type=="1"){
                            $scope.xjCoupon.push(data.res.coupon);
                        }else if(data.res.coupon.type=="2"){
                            $scope.mjCoupon.push(data.res.coupon);
                        }
                        $scope.couponNum=$scope.couponNum+1;
                        break;
                    case 352:
                        $scope.errContent="无效的兑换码";
                        break;
                    case 353:
                        $scope.errContent="已领取过该优惠券";
                        break;
                    case 354:
                        $scope.errContent="优惠券已过期";
                        break;
                    case 356:
                        $scope.errContent="优惠券已被领完";
                        break;
                }
                getCouponErr.modal();
            });
        };
        //扫描二维码领取
        $scope.scan=function(){
            wx.scanQRCode({
                needResult:1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType:["qrCode","barCode"],// 可以指定扫二维码还是一维码，默认二者都有
                success:function (res) {
                    var cid = res.resultStr;
                },
                fail:function(err){
                    alert(angular.toJson(err));
                }
            });
        };

}]);

grApp.factory('CouponFactory',['$http', '$q', function ($http, $q){
    var service={};
    //获取优惠券列表方法
    service.getUserCouponsList= function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'POST',
            url: '/user/center/getUserCouponsList'
            }).success(function(data, status, headers, config) {
                deferred.resolve(data);
            }).error(function(data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //兑换码领取优惠券
    service.getCouponCode= function (code) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'POST',
            url: '/user/center/getCouponCode',
            params:{code:code}
        }).success(function(data, status, headers, config) {
            //console.log(data);
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    return service;
}]);